package cn.jingsheng.backup.util;

import java.io.UnsupportedEncodingException;

/**
 * 字符串处理相关类
 * 
 * @author 李晓丹
 * 
 */
public class StringUtil {
	/**
	 * 去除字符串两边空格 当参数为空时 返回空字符串
	 * 
	 * @param str
	 * @return
	 */
	public static String trimToEmpty(String str) {
		if (str == null) {
			return "";
		}
		return str.trim();
	}

	/**
	 * 字符串是否为空
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isEmpty(String str) {
		return str == null || str.length() == 0;
	}

	/**
	 * 字符串是否为空
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNotEmpty(String str) {
		return !isEmpty(str);
	}

	/**
	 * 检查格式,文件后缀
	 * 
	 * @param fileName
	 * @throws UnsupportedEncodingException
	 */
	public static String checkContentType_bak(String fileName) throws UnsupportedEncodingException {
		String contentType = null;

		String[] splitString = fileName.split("\\.");
		int length = splitString.length;
		// 文件后缀
		String format = null;
		if (length > 1) {
			format = splitString[length - 1].toLowerCase();
		}
		if ("xls".equals(format)) {
			// contentType设定
			contentType = "application/vnd.ms-excel;charset=utf-8";

		} else if ("xlsx".equals(format)) {
			// contentType设定
			contentType = "application/vnd.ms-excel;charset=utf-8";
		} else if ("pdf".equals(format)) {
			// contentType设定
			contentType = "application/pdf;charset=utf-8";
		} else if ("doc".equals(format)) {
			// contentType设定
			contentType = "application/msword;charset=utf-8";
		} else if ("docx".equals(format)) {
			// contentType设定
			contentType = "application/msword;charset=utf-8";
		} else if ("txt".equals(format)) {
			// contentType设定
			contentType = "text/plain;charset=utf-8";
		} else {
			// contentType设定
			contentType = "text/plain;charset=utf-8";
		}
		return contentType;
	}

	/**
	 * 检查格式,文件后缀
	 * 
	 * @param fileName
	 * @throws UnsupportedEncodingException
	 */
	public static String checkContentType(String fileName) throws UnsupportedEncodingException {
		String contentType = null;

		String[] splitString = fileName.split("\\.");
		int length = splitString.length;
		// 文件后缀
		String format = null;
		if (length > 1) {
			format = splitString[length - 1].toLowerCase();
		}
		/***/
		if ("ez".equals(format)) {
			contentType = "application/andrew-inset;charset=utf-8";
		} else if ("hqx".equals(format)) {
			contentType = "application/mac-binhex40;charset=utf-8";
		} else if ("cpt".equals(format)) {
			contentType = "application/mac-compactpro;charset=utf-8";
		} else if ("doc".equals(format)) {
			contentType = "application/msword;charset=utf-8";
		} else if ("bin".equals(format)) {
			contentType = "application/octet-stream;charset=utf-8";
		} else if ("dms".equals(format)) {
			contentType = "application/octet-stream;charset=utf-8";
		} else if ("lha".equals(format)) {
			contentType = "application/octet-stream;charset=utf-8";
		} else if ("lzh".equals(format)) {
			contentType = "application/octet-stream;charset=utf-8";
		} else if ("exe".equals(format)) {
			contentType = "application/octet-stream;charset=utf-8";
		} else if ("class".equals(format)) {
			contentType = "application/octet-stream;charset=utf-8";
		} else if ("so".equals(format)) {
			contentType = "application/octet-stream;charset=utf-8";
		} else if ("dll".equals(format)) {
			contentType = "application/octet-stream;charset=utf-8";
		} else if ("oda".equals(format)) {
			contentType = "application/oda;charset=utf-8";
		} else if ("pdf".equals(format)) {
			contentType = "application/pdf;charset=utf-8";
		} else if ("ai".equals(format)) {
			contentType = "application/postscript;charset=utf-8";
		} else if ("eps".equals(format)) {
			contentType = "application/postscript;charset=utf-8";
		} else if ("ps".equals(format)) {
			contentType = "application/postscript;charset=utf-8";
		} else if ("smi".equals(format)) {
			contentType = "application/smil;charset=utf-8";
		} else if ("smil".equals(format)) {
			contentType = "application/smil;charset=utf-8";
		} else if ("mif".equals(format)) {
			contentType = "application/vnd.mif;charset=utf-8";
		} else if ("xls".equals(format)) {
			contentType = "application/vnd.ms-excel;charset=utf-8";
		} else if ("ppt".equals(format)) {
			contentType = "application/vnd.ms-powerpoint;charset=utf-8";
		} else if ("wbxml".equals(format)) {
			contentType = "application/vnd.wap.wbxml;charset=utf-8";
		} else if ("wmlc".equals(format)) {
			contentType = "application/vnd.wap.wmlc;charset=utf-8";
		} else if ("wmlsc".equals(format)) {
			contentType = "application/vnd.wap.wmlscriptc;charset=utf-8";
		} else if ("bcpio".equals(format)) {
			contentType = "application/x-bcpio;charset=utf-8";
		} else if ("vcd".equals(format)) {
			contentType = "application/x-cdlink;charset=utf-8";
		} else if ("pgn".equals(format)) {
			contentType = "application/x-chess-pgn;charset=utf-8";
		} else if ("cpio".equals(format)) {
			contentType = "application/x-cpio;charset=utf-8";
		} else if ("csh".equals(format)) {
			contentType = "application/x-csh;charset=utf-8";
		} else if ("dcr".equals(format)) {
			contentType = "application/x-director;charset=utf-8";
		} else if ("dir".equals(format)) {
			contentType = "application/x-director;charset=utf-8";
		} else if ("dxr".equals(format)) {
			contentType = "application/x-director;charset=utf-8";
		} else if ("dvi".equals(format)) {
			contentType = "application/x-dvi;charset=utf-8";
		} else if ("spl".equals(format)) {
			contentType = "application/x-futuresplash;charset=utf-8";
		} else if ("gtar".equals(format)) {
			contentType = "application/x-gtar;charset=utf-8";
		} else if ("hdf".equals(format)) {
			contentType = "application/x-hdf;charset=utf-8";
		} else if ("js".equals(format)) {
			contentType = "application/x-javascript;charset=utf-8";
		} else if ("skp".equals(format)) {
			contentType = "application/x-koan;charset=utf-8";
		} else if ("skd".equals(format)) {
			contentType = "application/x-koan;charset=utf-8";
		} else if ("skt".equals(format)) {
			contentType = "application/x-koan;charset=utf-8";
		} else if ("skm".equals(format)) {
			contentType = "application/x-koan;charset=utf-8";
		} else if ("latex".equals(format)) {
			contentType = "application/x-latex;charset=utf-8";
		} else if ("nc".equals(format)) {
			contentType = "application/x-netcdf;charset=utf-8";
		} else if ("cdf".equals(format)) {
			contentType = "application/x-netcdf;charset=utf-8";
		} else if ("sh".equals(format)) {
			contentType = "application/x-sh;charset=utf-8";
		} else if ("shar".equals(format)) {
			contentType = "application/x-shar;charset=utf-8";
		} else if ("swf".equals(format)) {
			contentType = "application/x-shockwave-flash;charset=utf-8";
		} else if ("sit".equals(format)) {
			contentType = "application/x-stuffit;charset=utf-8";
		} else if ("sv4cpio".equals(format)) {
			contentType = "application/x-sv4cpio;charset=utf-8";
		} else if ("sv4crc".equals(format)) {
			contentType = "application/x-sv4crc;charset=utf-8";
		} else if ("tar".equals(format)) {
			contentType = "application/x-tar;charset=utf-8";
		} else if ("tcl".equals(format)) {
			contentType = "application/x-tcl;charset=utf-8";
		} else if ("tex".equals(format)) {
			contentType = "application/x-tex;charset=utf-8";
		} else if ("texinfo".equals(format)) {
			contentType = "application/x-texinfo;charset=utf-8";
		} else if ("texi".equals(format)) {
			contentType = "application/x-texinfo;charset=utf-8";
		} else if ("t".equals(format)) {
			contentType = "application/x-troff;charset=utf-8";
		} else if ("tr".equals(format)) {
			contentType = "application/x-troff;charset=utf-8";
		} else if ("roff".equals(format)) {
			contentType = "application/x-troff;charset=utf-8";
		} else if ("man".equals(format)) {
			contentType = "application/x-troff-man;charset=utf-8";
		} else if ("me".equals(format)) {
			contentType = "application/x-troff-me;charset=utf-8";
		} else if ("ms".equals(format)) {
			contentType = "application/x-troff-ms;charset=utf-8";
		} else if ("ustar".equals(format)) {
			contentType = "application/x-ustar;charset=utf-8";
		} else if ("src".equals(format)) {
			contentType = "application/x-wais-source;charset=utf-8";
		} else if ("xhtml".equals(format)) {
			contentType = "application/xhtml+xml;charset=utf-8";
		} else if ("xht".equals(format)) {
			contentType = "application/xhtml+xml;charset=utf-8";
		} else if ("zip".equals(format)) {
			contentType = "application/zip;charset=utf-8";
		} else if ("au".equals(format)) {
			contentType = "audio/basic;charset=utf-8";
		} else if ("snd".equals(format)) {
			contentType = "audio/basic;charset=utf-8";
		} else if ("mid".equals(format)) {
			contentType = "audio/midi;charset=utf-8";
		} else if ("midi".equals(format)) {
			contentType = "audio/midi;charset=utf-8";
		} else if ("kar".equals(format)) {
			contentType = "audio/midi;charset=utf-8";
		} else if ("mpga".equals(format)) {
			contentType = "audio/mpeg;charset=utf-8";
		} else if ("mp2".equals(format)) {
			contentType = "audio/mpeg;charset=utf-8";
		} else if ("mp3".equals(format)) {
			contentType = "audio/mpeg;charset=utf-8";
		} else if ("aif".equals(format)) {
			contentType = "audio/x-aiff;charset=utf-8";
		} else if ("aiff".equals(format)) {
			contentType = "audio/x-aiff;charset=utf-8";
		} else if ("aifc".equals(format)) {
			contentType = "audio/x-aiff;charset=utf-8";
		} else if ("m3u".equals(format)) {
			contentType = "audio/x-mpegurl;charset=utf-8";
		} else if ("ram".equals(format)) {
			contentType = "audio/x-pn-realaudio;charset=utf-8";
		} else if ("rm".equals(format)) {
			contentType = "audio/x-pn-realaudio;charset=utf-8";
		} else if ("rpm".equals(format)) {
			contentType = "audio/x-pn-realaudio-plugin;charset=utf-8";
		} else if ("ra".equals(format)) {
			contentType = "audio/x-realaudio;charset=utf-8";
		} else if ("wav".equals(format)) {
			contentType = "audio/x-wav;charset=utf-8";
		} else if ("pdb".equals(format)) {
			contentType = "chemical/x-pdb;charset=utf-8";
		} else if ("xyz".equals(format)) {
			contentType = "chemical/x-xyz;charset=utf-8";
		} else if ("bmp".equals(format)) {
			contentType = "image/bmp;charset=utf-8";
		} else if ("gif".equals(format)) {
			contentType = "image/gif;charset=utf-8";
		} else if ("ief".equals(format)) {
			contentType = "image/ief;charset=utf-8";
		} else if ("jpeg".equals(format)) {
			contentType = "image/jpeg;charset=utf-8";
		} else if ("jpg".equals(format)) {
			contentType = "image/jpeg;charset=utf-8";
		} else if ("jpe".equals(format)) {
			contentType = "image/jpeg;charset=utf-8";
		} else if ("png".equals(format)) {
			contentType = "image/png;charset=utf-8";
		} else if ("tiff".equals(format)) {
			contentType = "image/tiff;charset=utf-8";
		} else if ("tif".equals(format)) {
			contentType = "image/tiff;charset=utf-8";
		} else if ("djvu".equals(format)) {
			contentType = "image/vnd.djvu;charset=utf-8";
		} else if ("djv".equals(format)) {
			contentType = "image/vnd.djvu;charset=utf-8";
		} else if ("wbmp".equals(format)) {
			contentType = "image/vnd.wap.wbmp;charset=utf-8";
		} else if ("ras".equals(format)) {
			contentType = "image/x-cmu-raster;charset=utf-8";
		} else if ("pnm".equals(format)) {
			contentType = "image/x-portable-anymap;charset=utf-8";
		} else if ("pbm".equals(format)) {
			contentType = "image/x-portable-bitmap;charset=utf-8";
		} else if ("pgm".equals(format)) {
			contentType = "image/x-portable-graymap;charset=utf-8";
		} else if ("ppm".equals(format)) {
			contentType = "image/x-portable-pixmap;charset=utf-8";
		} else if ("rgb".equals(format)) {
			contentType = "image/x-rgb;charset=utf-8";
		} else if ("xbm".equals(format)) {
			contentType = "image/x-xbitmap;charset=utf-8";
		} else if ("xpm".equals(format)) {
			contentType = "image/x-xpixmap;charset=utf-8";
		} else if ("xwd".equals(format)) {
			contentType = "image/x-xwindowdump;charset=utf-8";
		} else if ("igs".equals(format)) {
			contentType = "model/iges;charset=utf-8";
		} else if ("iges".equals(format)) {
			contentType = "model/iges;charset=utf-8";
		} else if ("msh".equals(format)) {
			contentType = "model/mesh;charset=utf-8";
		} else if ("mesh".equals(format)) {
			contentType = "model/mesh;charset=utf-8";
		} else if ("silo".equals(format)) {
			contentType = "model/mesh;charset=utf-8";
		} else if ("wrl".equals(format)) {
			contentType = "model/vrml;charset=utf-8";
		} else if ("vrml".equals(format)) {
			contentType = "model/vrml;charset=utf-8";
		} else if ("css".equals(format)) {
			contentType = "text/css;charset=utf-8";
		} else if ("html".equals(format)) {
			contentType = "text/html;charset=utf-8";
		} else if ("htm".equals(format)) {
			contentType = "text/html;charset=utf-8";
		} else if ("asc".equals(format)) {
			contentType = "text/plain;charset=utf-8";
		} else if ("txt".equals(format)) {
			contentType = "text/plain;charset=utf-8";
		} else if ("rtx".equals(format)) {
			contentType = "text/richtext;charset=utf-8";
		} else if ("rtf".equals(format)) {
			contentType = "text/rtf;charset=utf-8";
		} else if ("sgml".equals(format)) {
			contentType = "text/sgml;charset=utf-8";
		} else if ("sgm".equals(format)) {
			contentType = "text/sgml;charset=utf-8";
		} else if ("tsv".equals(format)) {
			contentType = "text/tab-separated-values;charset=utf-8";
		} else if ("wml".equals(format)) {
			contentType = "text/vnd.wap.wml;charset=utf-8";
		} else if ("wmls".equals(format)) {
			contentType = "text/vnd.wap.wmlscript;charset=utf-8";
		} else if ("etx".equals(format)) {
			contentType = "text/x-setext;charset=utf-8";
		} else if ("xsl".equals(format)) {
			contentType = "text/xml;charset=utf-8";
		} else if ("xml".equals(format)) {
			contentType = "text/xml;charset=utf-8";
		} else if ("mpeg".equals(format)) {
			contentType = "video/mpeg;charset=utf-8";
		} else if ("mpg".equals(format)) {
			contentType = "video/mpeg;charset=utf-8";
		} else if ("mpe".equals(format)) {
			contentType = "video/mpeg;charset=utf-8";
		} else if ("qt".equals(format)) {
			contentType = "video/quicktime;charset=utf-8";
		} else if ("mov".equals(format)) {
			contentType = "video/quicktime;charset=utf-8";
		} else if ("mxu".equals(format)) {
			contentType = "video/vnd.mpegurl;charset=utf-8";
		} else if ("avi".equals(format)) {
			contentType = "video/x-msvideo;charset=utf-8";
		} else if ("movie".equals(format)) {
			contentType = "video/x-sgi-movie;charset=utf-8";
		} else if ("ice".equals(format)) {
			contentType = "x-conference/x-cooltalk";
		} else if ("xlsx".equals(format)) {
			// contentType设定
			contentType = "application/vnd.ms-excel;charset=utf-8";
		} else if ("pdf".equals(format)) {
			// contentType设定
			contentType = "application/pdf;charset=utf-8";
		} else if ("doc".equals(format)) {
			// contentType设定
			contentType = "application/msword;charset=utf-8";
		} else if ("docx".equals(format)) {
			// contentType设定
			contentType = "application/msword;charset=utf-8";
		} else if ("txt".equals(format)) {
			// contentType设定
			contentType = "text/plain;charset=utf-8";
		} else {
			// contentType设定
			contentType = "text/plain;charset=utf-8";
		}
		/***/
		return contentType;
	}
}
